//
//  ResetLoginPasswordVC.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/21.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "ResetLoginPasswordVC.h"
#import "UIButton+Timer.h"
#import "NSString+Library.h"
#import "GKLTextFieldLabelItem.h"
#import "GKAutorizationCodeItem.h"
@interface ResetLoginPasswordVC ()
@property(nonatomic, strong) UIScrollView   *scrollView;
/** 控件容器*/
@property(nonatomic, strong) UIView   *itemContainters;
/** 验证码*/
@property(nonatomic, strong) GKAutorizationCodeItem   *autorizationCodeItem;
/** 新密码*/
@property(nonatomic, strong) GKLTextFieldLabelItem   *nPasswordItem;
/** 确认修改button*/
@property(nonatomic, strong) UIButton   *confirmButton;
@end

@implementation ResetLoginPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self.view addSubview:self.scrollView];
        [self.scrollView addSubview:self.itemContainters];
        [self.itemContainters addSubview:self.autorizationCodeItem];
        [self.itemContainters addSubview:self.nPasswordItem];
        [self.itemContainters addSubview:self.confirmButton];
        [self createConstraints];
    }
    return self;
}

#pragma mark - constraints

- (void)createConstraints
{
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.itemContainters mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.scrollView);
        make.top.equalTo(self.scrollView.mas_top).offset(10);
        make.width.equalTo(self.scrollView);
    }];
    
    [self.autorizationCodeItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.itemContainters);
        make.height.equalTo(@44);
    }];
    
    [self.nPasswordItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.itemContainters);
        make.top.equalTo(self.autorizationCodeItem.mas_bottom).offset(1);
        make.height.equalTo(@44);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView.mas_left).offset(12);
        make.top.equalTo(self.nPasswordItem.mas_bottom).offset(12);
        make.height.equalTo(@44);
        make.width.equalTo(@(kScreenWidth - 24));
        make.bottom.equalTo(self.scrollView.mas_bottom);
    }];
}

#pragma mark - 计算title

- (CGFloat)calculateTitleMaxWidth
{
    NSString *str2 = @"请输入新登录密码";
    return [str2 sizeWithFont:[UIFont systemFontOfSize:14]].width;
}
- (CGFloat)calculateTitleMaxWidth1
{
    NSString *str2 = @"请输入验证码";
    return [str2 sizeWithFont:[UIFont systemFontOfSize:14]].width;
}

#pragma mark - net
/** 发送验证码*/
- (void)handleSendAutoriationCodeRequest
{
    [self.autorizationCodeItem.timeButton timeCount:60];
    [self code];
}

#pragma mark - action
- (void)timeButtonAction:(UIButton *)sender
{
    [self handleSendAutoriationCodeRequest];
}

- (void)confirmButtonAction:(UIButton *)sender
{
//    NSString *msg = [self checkResetPassword];
//    if (msg.length == 0) {
//        [self handleResetPasswordRequest];
//    } else {
//        [MBProgressHUD show];
//    }
    [self resetPassword];
}
#pragma mark - getter
- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.alwaysBounceVertical = YES;
        _scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    }
    return _scrollView;
}

- (UIView *)itemContainters
{
    if (_itemContainters == nil) {
        _itemContainters = [[UIView alloc] init];
    }
    return _itemContainters;
}
- (GKAutorizationCodeItem *)autorizationCodeItem
{
    if (_autorizationCodeItem == nil) {
        _autorizationCodeItem = [[GKAutorizationCodeItem alloc] initWithTitle:@"验证码:" titleMaxWidth:[self calculateTitleMaxWidth1] placeHolder:@"请输入验证码"];
        _autorizationCodeItem.keyboardType = UIKeyboardTypeDecimalPad;
        __weak typeof(self) weakSelf = self;
        _autorizationCodeItem.callBack = ^(NSString *text) {
            if (text.length > 0 && weakSelf.nPasswordItem.content.length > 0) {
                weakSelf.confirmButton.selected = YES;
                weakSelf.confirmButton.userInteractionEnabled = YES;
            } else {
                weakSelf.confirmButton.selected = NO;
                weakSelf.confirmButton.userInteractionEnabled = NO;
            }
        };
        [_autorizationCodeItem.timeButton addTarget:self action:@selector(timeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _autorizationCodeItem;
}

- (GKLTextFieldLabelItem *)nPasswordItem
{
    NSString *titleText;
    if ([self.title isEqualToString:@"重置登录密码"]) {
        titleText = @"新登录密码";
    }else {
        titleText = @"新交易密码";
    }
    if (_nPasswordItem == nil) {
        _nPasswordItem = [[GKLTextFieldLabelItem alloc] initWithTitle:titleText titleMaxWidth:[self calculateTitleMaxWidth] placeHolder:@"密码长度8-16位，必须包含字母"];
        __weak typeof(self) weakSelf = self;
        _nPasswordItem.keyboardType = UIKeyboardTypeDefault;
        _nPasswordItem.secureTextEntry = YES;
        _nPasswordItem.callBack = ^(NSString *text) {
            if (text.length > 0 && weakSelf.autorizationCodeItem.content.length > 0) {
                weakSelf.confirmButton.selected = YES;
                weakSelf.confirmButton.userInteractionEnabled = YES;
            } else {
                weakSelf.confirmButton.selected = NO;
                weakSelf.confirmButton.userInteractionEnabled = NO;
            }
        };
    }
    return _nPasswordItem;
}

- (UIButton *)confirmButton
{
    if (_confirmButton == nil) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmButton setTitle:@"确认修改" forState:UIControlStateNormal];
        _confirmButton.backgroundColor = [UIColor custom_color:@"#A91E23"];
        [_confirmButton addTarget:self action:@selector(confirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _confirmButton.layer.cornerRadius = 22;
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _confirmButton.layer.masksToBounds = YES;
    }
    return _confirmButton;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark --- 获取验证码
- (void)code {
    NSString *url;
    NSDictionary *dic;
    url = [[apiManger shareApiManger] GetVerificatioVode];
    dic = @{
            @"phoneReg":[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]
            };

    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (void)resetPassword {
    if ([self.autorizationCodeItem.content isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:@"请输入验证码"];
    }
    if ([self.nPasswordItem.content isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:@"请设置密码"];
    }
    NSString *result = [self.nPasswordItem.content transactionPasswordReguler];
    if (![result isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:result];
        return;
    }
    NSString *url;
    NSDictionary *dic;
    if ([self.title isEqualToString:@"重置交易密码"]) {
        url = [[apiManger shareApiManger] resetradPassword];
        dic = @{
                @"token":USERTOKEN,
                @"codeReq":self.autorizationCodeItem.content,
                @"newPassword":self.nPasswordItem.content
                };
    }else {//重置登录密码
        url = [[apiManger shareApiManger] resetAndForgetLogInPassword];
        dic = @{
                @"phone":[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"],
                @"codeReg":self.autorizationCodeItem.content,
                @"newPassword":self.nPasswordItem.content
                };
    }
   
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
